﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
// add
using System.Collections;


namespace FormBaseLib
{
    /// <summary>
    /// Independand defination implemented of our own List-Key-Value in string format!
    /// </summary>
    public class CKeyValues : CBase, ICRUD, IEnumerable
    {
        private CKeyedCollectionKeyValue mCollection { get; set; }

        // Row Buffer.
        //public string msKey { get; set; }
        //public string msValue { get; set; }
        public CItemKeyValue mRow { get; set; }
        public int miCount { get { return mCollection.Count; } }

        public CKeyValues()
        {
            mCollection = new CKeyedCollectionKeyValue();
            mRow = new CItemKeyValue();
            mRow.Clear();
        }
        public bool FindKey()
        {
            if (string.IsNullOrEmpty(mRow.msKey))
            {
                SetLastError(enumErrorNo.ErrorEmptyOrNull, "Key is empty or null.");
                return false;
            }
            if (!mCollection.Contains(mRow.msKey))
                return false;

            return false;
        }
        public bool Create()
        {
            if (string.IsNullOrEmpty(mRow.msKey))
            {
                SetLastError(enumErrorNo.ErrorEmptyOrNull, "Key is empty or null.");
                return false;
            }
            if (FindKey())
            {
                SetLastError(enumErrorNo.ErrorExist, string.Format("Duplicated key={0}.", mRow.msKey));
                return false;
            }
            mCollection.Add(new CItemKeyValue(mRow.msKey, mRow.msValue));
            return true;
        }
        public int CreateFromStringSeparator(string sInput, string sSeparator)
        {
            return mRow.SetFromStringSeparator(sInput, sSeparator);
        }
        public bool Read()
        {
            if (!FindKey())
            {
                SetLastError(enumErrorNo.ErrorNotExist, string.Format("Key does NOT exist, key={0}", mRow.msKey));
                return false;
            }
            mRow.SetFrom(mCollection[mRow.msKey]);
            return true;
        }
        public bool Update()
        {
            if (!FindKey())
            {
                SetLastError(enumErrorNo.ErrorNotExist, string.Format("Key does NOT exist, key={0}", mRow.msKey));
                return false;
            }
            mCollection[mRow.msKey].SetFrom(mRow);
            return true;
        }
        public bool Delete()
        {
            if (!FindKey())
            {
                SetLastError(enumErrorNo.ErrorNotExist, string.Format("Key does NOT exist, key={0}", mRow.msKey));
                return false;
            }
            return mCollection.Remove(mRow.msKey);
        }
        public void DeleteAllRows()
        {
            mCollection.Clear();
        }
        public void ClearRowBuffer()
        {
            mRow.Clear();
        }
        public bool IsSameAllRows(CKeyValues pTarget)
        {
            if (miCount != pTarget.miCount)
                return false;
            
            foreach (CItemKeyValue itemEach in mCollection)
            {
                pTarget.mRow.msKey = itemEach.msKey;
                if (!pTarget.Read())
                    return false;

                if (itemEach.msValue != pTarget.mRow.msValue)
                    return false;
            }
            return true;
        }
        public string[] GetArrayKeys()
        {
            int i;
            string[] sa1 = new string[miCount];

            i = 0;
            foreach (CItemKeyValue itemP in mCollection)
            {
                sa1[i] = itemP.msKey;
                i++;
            }
            return sa1;
        }
        public string[] GetArrayValues()
        {
            int i;
            string[] sa1 = new string[miCount];

            i = 0;
            foreach (CItemKeyValue itemP in mCollection)
            {
                sa1[i] = itemP.msValue;
                i++;
            }
            return sa1;
        }

        /// <summary>
        /// ToStringArray of (Row, Col)
        /// Col 0 = Key
        /// Col 1 = Value 
        /// </summary>
        /// <returns></returns>
        public string[,] ToStringArray()
        {
            int i;
            string[,] sa1 = new string[miCount,2];

            i = 0;
            foreach (CItemKeyValue itemP in mCollection)
            {
                sa1[i, 0] = itemP.msKey;
                sa1[i, 1] = itemP.msValue;
                i++;
            }
            return sa1;
        }

        public bool SetAllRows(CKeyValues pFrom)
        {
            mCollection.Clear();
            if (pFrom.miCount < 1)
                return true;

            foreach (CItemKeyValue itemEach in pFrom.mCollection)
            {
                mRow.SetFrom(itemEach);
                if (!Create())
                    return false;
            }
            mRow.SetFrom(pFrom.mRow);
            return true;
        }
        public void CopyTo(CDatasetKeyValue pTarget)
        {
            pTarget.DeleteAllRows();
            if (this.miCount < 1)
                return;

            foreach (CItemKeyValue itemEach in mCollection)
            {
                pTarget.mRow.FKey = itemEach.msKey;
                pTarget.mRow.FValue = itemEach.msValue;
                pTarget.RowCreate(true);
            }
        }


        public IEnumerator GetEnumerator()
        {
            return mCollection.GetEnumerator();
        }
    }
}
